﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

namespace graphics
{
    public partial class UserControlpaint : UserControl
    {
        private Point startpoint;
        private Point oldpoint;
        private bool isdraw;
        private bool isclear;

        public UserControlpaint()
        {
            InitializeComponent();

            b = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
            g = Graphics.FromImage(b);
            g.Clear(Color.White);

            g1 = this.CreateGraphics();
            this.BackColor = Color.White;

            //g1.Clear(Color.White);

            //Graphics g1 = this.CreateGraphics();
            //g1.DrawImage(b, this.ClientRectangle);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            g1.DrawRectangle(new Pen(Color.Black, 5), this.ClientRectangle);
            base.OnPaint(e);
        }

        private void UserControlpaint_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isdraw = true;
                isclear = false;
                startpoint = new Point(e.X, e.Y);
                oldpoint = new Point(e.X, e.Y);
            }
            else if (e.Button == MouseButtons.Right)
            {
                isclear = true;
                isdraw = false;
                startpoint = new Point(e.X, e.Y);
                oldpoint = new Point(e.X, e.Y);
            }
            //else if(e.Button==MouseButtons.)
        }

        private Bitmap b;
        private Graphics g;
        private Graphics g1;

        private void UserControlpaint_MouseMove(object sender, MouseEventArgs e)
        {
            if (isdraw)
            {
                g.DrawLine(new Pen(Color.Black, 1), oldpoint, new Point(e.X, e.Y));
                g1.DrawLine(new Pen(Color.Black, 1), oldpoint, new Point(e.X, e.Y));

                oldpoint.X = e.X;
                oldpoint.Y = e.Y;
            }
            else if (isclear)
            {
                g.DrawLine(new Pen(Color.White, 35), oldpoint, new Point(e.X, e.Y));
                g1.DrawLine(new Pen(Color.White, 35), oldpoint, new Point(e.X, e.Y));

                oldpoint.X = e.X;
                oldpoint.Y = e.Y;
            }
        }

        private void UserControlpaint_MouseUp(object sender, MouseEventArgs e)
        {
            isdraw = false;
        }

        private void UserControlpaint_Load(object sender, EventArgs e)
        {
            this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            //g1.DrawRectangle(Pens.Black, 0, 0, this.Width - 10, this.Height - 10);
        }

        private void UserControlpaint_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.S && e.Modifiers == Keys.Control)
            {
                //MessageBox.Show("ok");
                if (b != null)
                {
                    string strFileName = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace(":", "");
                    string filename = strFileName + ".jpg";
                    b.Save(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), filename), ImageFormat.Jpeg);
                }
            }
        }

        private void UserControlpaint_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //e.s = true; // don't pass the event down
            if (e.Button == MouseButtons.Right)
            {
                g.Clear(Color.White);
                g1.Clear(Color.White);
            }
        }
    }
}